#include <cstring>
#include "../Basics/BasicStruct.cpp"

Tree CreateTreeByFile(string filename) {
    string path = "C:\\Users\\Administrator\\CLionProjects\\RealQuestion\\Tree-Question\\";
    string realPath = path+filename;
    FILE* fq=fopen(realPath.c_str() ,"r");
    queue<TreeNode*>queue;
    TreeNode * treeNode = (TreeNode*) malloc(sizeof(TreeNode));
    fscanf(fq,"%d",&treeNode->data);
    treeNode->leftChild = NULL;
    treeNode->rightChild = NULL;
    queue.push(treeNode);
    int left;int right;
    while(fscanf(fq,"%d %d",&left,&right) != EOF){
        if (left == -1 && right == -1 ){
            continue;
        }
        TreeNode *p = queue.front();
        queue.pop();
        TreeNode * leftNode = (TreeNode*) malloc(sizeof(TreeNode));
        if (left != -1) {
            leftNode->data = left;
            leftNode->rightChild = NULL;
            leftNode->leftChild = NULL;
            p->leftChild = leftNode;
            queue.push(leftNode);
        }
        TreeNode * rightNode = (TreeNode*) malloc(sizeof(TreeNode));
        if (right != -1) {
            rightNode->data = right;
            rightNode->rightChild = NULL;
            rightNode->leftChild = NULL;
            p->rightChild = rightNode;
            queue.push(rightNode);
        }
    }
    return treeNode;
}



